package com.uchengwang.ui.cm_ui.cycle.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.cycle.adapter.CycleGroupAdapter;
import com.uchengwang.ui.cm_ui.cycle.bean.RyGroupBean;
import com.uchengwang.ui.cm_ui.rychat.manager.RyGroupManager;

import java.util.List;

/**
 * Created by U城网 on 2018/1/22.
 * 群组界面 社圈或者别的
 */

public class CycleGroupsActivity extends BaseWithTitleActivity{
    private PtrClassicFrameLayout layout;
    private RecyclerView rv;
    private CycleGroupAdapter adapter;
    private RecyclerAdapterWithHF mAdapter;
    private int page, totlaPagers;
    private long cycleId;
    private String cycleName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_group_layou);
        cycleId = getIntent().getLongExtra(BaseConfig.ID, 0);
        cycleName = getIntent().getStringExtra(BaseConfig.PUB_TITLE);
        setTitle(cycleName+"群");
        initView();

//        setRightImage(R.drawable.image_menu, new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//
//            }
//        });

    }

    private void initView(){
        layout = (PtrClassicFrameLayout) findViewById(R.id.fm_group_layout);
        rv = (RecyclerView) findViewById(R.id.fm_group_rv);

        initData();
    }

    private void initData(){
        adapter = new CycleGroupAdapter(this);
        mAdapter = new RecyclerAdapterWithHF(adapter);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(mAdapter);

        layout.setAutoLoadMoreEnable(true);
        layout.setLoadMoreEnable(true);

        layout.postDelayed(new Runnable() {

            @Override
            public void run() {
                layout.autoRefresh(true);
            }
        }, 150);

        layout.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                layout.loadMoreComplete(true);
                getGroups(true);
            }
        });

        layout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                page++;
                if (page <= totlaPagers){
                    //当前请求页大于总页数，不再请求
                    getGroups(false);
                } else {
                    layout.loadMoreComplete(false);
                }
            }
        });
    }

    /**
     * 获取一个社圈的群组
     * */
    private void getGroups(final boolean isRefresh){
        RyGroupManager.getManager().getCycleGroup(this, page, 10, cycleId, new OnHttpResultListener<RyGroupBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<RyGroupBean> list) {
                initGroupData(isRefresh, status, total, list);
            }
        });
    }

    private void initGroupData(boolean isRefresh, int stutas, int total, List<RyGroupBean> list){
        totlaPagers =total;
        if (isRefresh){
            layout.refreshComplete();
        }else {
            layout.loadMoreComplete(true);
        }
        if (stutas ==1){
            adapter.getData().clear();
            adapter.notifyDataSetChanged();
        }
        if (list != null && list.size() >0){
            if (isRefresh){
                adapter.setData(list);
            }else {
                adapter.getData().addAll(list);
                adapter.notifyDataSetChanged();
            }
        }
    }
}
